﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;

namespace PCWatch
{
	public partial class Form1: Form
	{
		public Form1()
		{
			InitializeComponent();
		}
        string file;
        private void Form1_Load(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;
            string[] ports = SerialPort.GetPortNames();
            Array.Sort(ports);
            cbCom.Items.Clear();
            cbCom.Items.AddRange(ports);
            cbCom.SelectedIndex = 0;
            file = System.Windows.Forms.Application.StartupPath + "\\TextLog\\" + DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".txt";
            FileInfo f = new FileInfo(file);
            if (!Directory.Exists(f.DirectoryName))
                Directory.CreateDirectory(f.DirectoryName);
            FileStream fs = new FileStream(file, FileMode.Create);
            fs.Close();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if(toolStripButton1.Text=="开始")
            {
                try
                {
                    serialPort1.PortName = cbCom.Text;
                    serialPort1.Open();
                }
                catch (System.Exception ex)
                {

                }
                toolStripButton1.Text = "结束";
            }
            else
            {
                serialPort1.Close();
                toolStripButton1.Text = "开始";
            }
        }
        byte[] RxList = new byte[21];
        int RxCount = 0;
        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int rxn = serialPort1.BytesToRead;
            while (rxn != 0)
            {
                rxn--;
                byte rx = (byte)serialPort1.ReadByte();
                RxList[RxCount++] = rx;
                if (RxCount == 1)
                {
                    if (RxList[0] != 0x16)
                    {
                        RxCount = 0;                                
                    }
                }
                else if (RxCount == 2)
                {
                    if (RxList[1] != 0x16)
                    {
                        RxCount=0;
                    }
                }
                else if (RxCount == 3)//命令
                {
                    if (RxList[2] != 0x02)
                    {
                        RxCount=0;
                    }
                }                    
                else if (RxCount > 5)//判断是否接收完成
                {
                    if (RxCount == 21 && RxList[20]==0x0d)//正确完成
                    {
                        switch (RxList[6])
                        {
                            case 0:
                                textBox2.Text = "关机";
                                break;
                            case 1:
                                textBox2.Text = "通风";
                                break;
                            case 2:
                                textBox2.Text = "制冷";
                                break;
                            default:
                                textBox2.Text = "未知";
                                break;
                        }
                        switch (RxList[7])
                        {
                            case 0:
                                textBox3.Text = "关机";
                                break;
                            case 1:
                                textBox3.Text = "小风";
                                break;
                            case 2:
                                textBox3.Text = "中风";
                                break;
                            default:
                                textBox3.Text = "大风";
                                break;
                        }
                        float l;
                        l = RxList[8] << 8;
                        l += RxList[9];
                        l = l - 512;
                        l = l * 5 / 66;
                        textBox4.Text = l.ToString("f3");
                        l = RxList[10] << 8;
                        l += RxList[11];
                        l = l - 512;
                        l = l * 5 / 66;
                        textBox5.Text = l.ToString("f3");
                        l = RxList[12] << 8;
                        l += RxList[13];
                        l = l - 512;
                        l = l * 5 / 66;
                        textBox6.Text = l.ToString("f3");
                        l = RxList[14] << 8;
                        l += RxList[15];
                        l = l - 512;
                        l = l * 5 / 66;
                        textBox7.Text = l.ToString("f3");
                        switch (RxList[16])
                        {
                            case 0:
                                textBox8.Text = "关机";
                                break;
                            case 1:
                                textBox8.Text = "启动风机，等待启动压缩机";
                                break;
                            case 2:
                                textBox8.Text = "SVPWM";
                                break;
                            case 3:
                                textBox8.Text = "等待相位同步";
                                break;
                            case 4:
                                textBox8.Text = "切换完成";
                                break;
                            case 5:
                                textBox8.Text = "关闭压缩机，等待关闭风机";
                                break;
                            default:
                                textBox8.Text = "未知";
                                break;
                        }
                        if (GetBit(RxList[17], 3))
                            checkBox1.Checked = true;
                        else
                            checkBox1.Checked = false;
                        if (GetBit(RxList[17], 4))
                            checkBox2.Checked = true;
                        else
                            checkBox2.Checked = false;
                        if (GetBit(RxList[17], 5))
                            checkBox4.Checked = true;
                        else
                            checkBox4.Checked = false;
                        if (GetBit(RxList[17], 6))
                            checkBox3.Checked = true;
                        else
                            checkBox3.Checked = false;                        
                        if (GetBit(RxList[17], 7))
                            checkBox5.Checked = true;
                        if (RxList[18] == 0)
                            checkBox6.Checked = false;
                        else
                            checkBox6.Checked = true;
                        StringBuilder strB = new StringBuilder();
                        strB.Clear();
                        strB.Append(DateTime.Now.ToLongTimeString());
                        strB.Append(":  ");
                        foreach (byte b in RxList)
                        {
                            strB.Append(b.ToString("X2"));
                            strB.Append(' ');
                        }
                        FileStream fs = new FileStream(file, FileMode.Open);
                        StreamWriter sw = new StreamWriter(fs);
                        //开始写入
                        fs.Seek(0, SeekOrigin.End);
                        sw.WriteLine(strB.ToString());
                        //清空缓冲区
                        sw.Flush();
                        //关闭流
                        sw.Close();
                        fs.Close();/**/
                        strB.Append("\r\n");
                        if (textBox1.Text.Length > 30000)
                            textBox1.Clear();
                        textBox1.AppendText(strB.ToString());  
                        RxCount = 0;
                    }
                    else
                    {
                        if (RxCount > 21)
                        {
                            RxCount = 0;
                        }
                    }
                }                        
            }
        }
        public Boolean GetBit(byte b, int index)
        {
            if (index > 7)
                return false;
            if ((byte)((b & (1 << index)) >> index) == 0x01)
                return true;
            else
                return false;
        }
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
        }    
	}
}
